package com.green.javagreen.controller;

import com.green.javagreen.bean.AdminUserBean;
import com.green.javagreen.bean.ResultBean;
import com.green.javagreen.injections.CurrentUser;
import com.green.javagreen.injections.PermissionCheck;
import com.green.javagreen.service.AdminUserService;
import com.green.javagreen.util.TextUtil;
import com.green.javagreen.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 */
@Api(description = "AdminUserBean管理")
@RestController
@RequestMapping("/admin_user")
public class AdminUserController {

    private AdminUserService adminUserService;

    private Logger logger = LoggerFactory.getLogger(AdminUserController.class);


    @Autowired
    public void setAdminUserService(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @ApiOperation(value = "获取列表")
    @PostMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean list(@CurrentUser AdminUserBean currentUser, @RequestBody AdminUserBean adminUserBean) {
        System.out.println("管理员账号:"+currentUser.getCode());
        Page<AdminUserBean> page = adminUserService.find(adminUserBean);
        return ResultBean.returnPage(page);
    }

    @ApiOperation(value = "保存 新增/修改")
    @PutMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean save(@RequestBody AdminUserBean adminUserBean) {
        adminUserService.save(adminUserBean);
        return ResultBean.returnSuccess(null);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean delete(@PathVariable int id) {
        adminUserService.delete(id);
        return ResultBean.returnSuccess(null);
    }


    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public ResultBean login(@RequestBody AdminUserBean adminUserBean) {
        String loginCode = adminUserBean.getCode();
        String loginPassword = adminUserBean.getPassWord();
        logger.debug("管理员登陆账号：", loginCode);
        logger.debug("管理员登陆密码：", loginPassword);

        // 校验合法
        if (TextUtil.isNotValid(loginCode)) {
            return ResultBean.returnFailed("登陆账号不能为空");
        }
        if (TextUtil.isNotValid(loginPassword)) {
            return ResultBean.returnFailed("登陆密码不能为空");
        }
        // 获取数据库内的管理员信息
        AdminUserBean exitBean = adminUserService.findByCode(loginCode);
        if (exitBean == null) {
            return ResultBean.returnFailed("账号不存在");
        }
        if (!exitBean.getPassWord().equals(loginPassword)) {
            return ResultBean.returnFailed("密码错误");
        }
        // 返回信息
        String token = TokenUtils.createJwtToken(loginCode);
        exitBean.setToken(token);
        return ResultBean.returnSuccess(exitBean, new String[]{
                "name",
                "code",
                "token"
        });
    }

}
